<?php
include_once(Utils::getPath(array('repositories', 'challengeRepository.php')));
include_once(Utils::getPath(array('repositories', 'userRepository.php')));
include_once(Utils::getPath(array('repositories', 'messageRepository.php')));
include_once(Utils::getPath(array('repositories', 'solutionRepository.php')));
include_once(Utils::getPath(array('repositories', 'commentRepository.php')));

class Summary
{
	public static function getSummaryInfo($userId)
	{
		$userRep = new UserRepository();
		$friendIds = $userRep->getFriendsIds($userId, true, 0, 0);
		
		$list = array();
		
		$list = array_merge($list, self::getUsersUploadChallenge($friendIds, 1));
		$list = array_merge($list, self::getUsersCommentInUserChallenge($userId, $friendIds, 1));
                $list = array_merge($list, self::getUsersCommentInUserSolution($userId, $friendIds, 1));
		$list = array_merge($list, self::getUsersSolutionInUserChallenge($userId, $friendIds, 1));
		$list = array_merge($list, self::getUserNewMessages($userId, 10));
		$list = array_merge($list, self::getUserPendingFriends($userId, 10));

		usort($list, array( 'Summary', 'elementsComparatorByDateTime'));
		
		return $list;
	}
	
	public static function getProfile($userId, $profileId)
	{
		$userRep = new UserRepository();
		$friendIds = $userRep->getFriendsIds($userId, true, 0, 0);
		
		$list = array();
		
		$list = array_merge($list, self::getUsersUploadChallenge(array($profileId), 5));
		$list = array_merge($list, self::getUsersCommentInUserChallenge($userId, array($profileId), 5));
                $list = array_merge($list, self::getUsersCommentInUserSolution($userId, array($profileId), 5));
		$list = array_merge($list, self::getUsersSolutionInUserChallenge($userId, array($profileId), 5));
		
		usort($list, array( 'Summary', 'elementsComparatorByDateTime'));
		
		return $list;
	}
	
	public static function getMyProfile($userId)
	{
		$userRep = new UserRepository();
		$friendIds = $userRep->getFriendsIds($userId, true, 0, 0);
		
		$list = array();
		
		$list = array_merge($list, self::getUsersUploadChallenge($friendIds, 5));
		$list = array_merge($list, self::getUsersCommentInUserChallenge($userId, null, 5));
                $list = array_merge($list, self::getUsersCommentInUserSolution($userId, null, 5));
		$list = array_merge($list, self::getUsersSolutionInUserChallenge($userId, null, 5));

		usort($list, array( 'Summary', 'elementsComparatorByDateTime'));
		
		return $list;
	}
	
	private static function getUsersUploadChallenge($userIds, $count)
	{
		$list = array();
		
		if(count($userIds) > 0)
		{
			$userIdsStr = '';
			for($i = 0; $i < count($userIds) - 1; $i++)
			{
				$userIdsStr = $userIdsStr.$userIds[$i].',';
			}
			$userIdsStr = $userIdsStr.$userIds[count($userIds) - 1];
			
			$chagRep = new ChallengeRepository();
			$chagList = $chagRep->find(array(array('idAuthor IN ('.$userIdsStr.')')), 'Date', true, 0, $count);
			
			return self::convertChallenges($chagList);
		}
		return $list;
	}
	private static function getUsersCommentInUserChallenge($idUser, $userIds, $count)
	{
		$list = array();
                $filter = null;
		
		if(count($userIds) > 0)
		{
			$userIdsStr = '';
			for($i = 0; $i < count($userIds) - 1; $i++)
			{
				$userIdsStr = $userIdsStr.$userIds[$i].',';
			}
			$userIdsStr = $userIdsStr.$userIds[count($userIds) - 1];
			$filter = array(array('comments.idAuthor IN ('.$userIdsStr.') AND '));
		}
		$filter[] = array('comments.idAuthor <> '.$idUser);
		
                $commRep = new CommentRepository();
		$commList = $commRep->findByChallengeAuthor($idUser, $filter, 'Date', true, 0, $count);
		
		return self::convertChallengeComments($commList);
	}
	private static function getUsersSolutionInUserChallenge($idUser, $userIds, $count)
	{
		$list = array();
                $filter = null;
		
		if(count($userIds) > 0)
		{
			$userIdsStr = '';
			for($i = 0; $i < count($userIds) - 1; $i++)
			{
				$userIdsStr = $userIdsStr.$userIds[$i].',';
			}
			$userIdsStr = $userIdsStr.$userIds[count($userIds) - 1];
			$filter = array(array('solutions.idAuthor IN ('.$userIdsStr.') AND '));
		}
		$filter[] = array('solutions.idAuthor <> '.$idUser.' AND ');
                $filter[] = array('Valid IS NULL');
		
		$solRep = new SolutionRepository();
		$solList = $solRep->findByChallengeAuthor($idUser, $filter, null, 'Date', true, 0, $count);
		
		return self::convertSolutions($solList);
	}
	private static function getUsersCommentInUserSolution($idUser, $userIds, $count)
	{
		$list = array();
                $filter = null;
		
		if(count($userIds) > 0)
		{
			$userIdsStr = '';
			for($i = 0; $i < count($userIds) - 1; $i++)
			{
				$userIdsStr = $userIdsStr.$userIds[$i].',';
			}
			$userIdsStr = $userIdsStr.$userIds[count($userIds) - 1];
			$filter = array(array('comments.idAuthor IN ('.$userIdsStr.') AND '));
		}
		$filter[] = array('comments.idAuthor <> '.$idUser);
		
                $commRep = new CommentRepository();
		$commList = $commRep->findBySolutionAuthor($idUser, $filter, 'Date', true, 0, $count);
		
		return self::convertSolutionComments($commList);
	}
	private static function getUserSolutions($idUser, $count)
	{
		$solRep = new SolutionRepository();
		$solList = $solRep->findByAuthor($idUser, true, 'Date', true, 0, $count);
		
		return self::convertSolutions($solList);
	}
	private static function getUserMessages($idUser, $count)
	{
		$messageRep = new MessageRepository();
		$messageList = $messageRep->findByReceiver($idUser, null, 'Date', true, 0, $count);
		
		return self::convertMessages($messageList);
	}
	private static function getUserNewMessages($idUser, $limit = null)
	{
		$res = array();
		$messageRep = new MessageRepository();
		$messageRep->findByReceiver($idUser, false,null,null,0,1);
		$count = $messageRep->countLast();

		if($count>0)
		{
			if($count == 1){
				$elem = array();
				$elem['message'] = "You have <span>1</span> new message.";
			}else if(!is_null($limit) && $count>$limit){
				$elem = array();
				$elem['message'] = "You have more than <span>$limit</span> new messages.";
			}else{
				$elem = array();
				$elem['message'] = "You have <span>$count</span> new messages.";
			}
			$elem['url'] = Utils::getUrl("message/list.php?filter=in");
			$res[] = $elem;
		}		
		return $res;
	}
	private static function getUserPendingFriends($idUser, $limit = null)
	{
		$res = array();
		$userRep = new UserRepository();
		$count = $userRep->CountRequests($idUser);

		if($count>0)
		{
			if($count == 1){
				$elem = array();
				$elem['message'] = "You have <span>1</span> pending friend invitation.";
			}else if(!is_null($limit) && $count>$limit){
				$elem = array();
				$elem['message'] = "You have more than <span>$limit</span> pending friend invitations.";
			}else{
				$elem = array();
				$elem['message'] = "You have <span>$count</span> pending friend invitations.";
			}
			$elem['url'] = Utils::getUrl("user/list.php?filter=requests");
			$res[] = $elem;
		}		
		return $res;
	}
	private static function elementsComparatorByDateTime($eleA, $eleB)
	{
            if(isset($eleA['datetime']) && isset ($eleB['datetime']))
            {
                $timeA = strtotime($eleA['datetime']);
		$timeB = strtotime($eleB['datetime']);
		
		if ($timeA == $timeB)
			return 0;
		return $timeA < $timeB? 1: -1;
            }
            return 0;
	}
	
	private static function convertChallenges($chagList)
	{
		$list = array();
		$userRep = new UserRepository();
		
		for($i = 0; $i < count($chagList); $i++)
		{
			$user = $userRep->getById($chagList[$i]->getIdAuthor());
			$userName = $user->getUserName();
			$listItem = array();
			$listItem['userId'] = $chagList[$i]->getIdAuthor();
			$listItem['name'] = $userName;
			$listItem['image'] = $user->getImage();
			$listItem['datetime'] = $chagList[$i]->getDate();
			$listItem['message'] = "$userName has uploaded a new challenge.";
			$listItem['url'] = Utils::getUrl("challenge/view.php?id=").$chagList[$i]->getId();
			
			$list[] = $listItem;
		}
		return  $list;
	}
	private static function convertChallengeComments($commList)
	{
		$list = array();
		$userRep = new UserRepository();
		
		for($i = 0; $i < count($commList); $i++)
		{
			$user = $userRep->getById($commList[$i]->getIdAuthor());
			$userName = $user->getUserName();
			$listItem = array();
			$listItem['userId'] = $commList[$i]->getIdAuthor();
			$listItem['name'] = $userName;
			$listItem['image'] = $user->getImage();
			$listItem['datetime'] = $commList[$i]->getDate();
			$listItem['message'] = "$userName has commented one of your challenges.";
			$listItem['url'] = Utils::getUrl("challenge/view.php?id=".$commList[$i]->getIdChallenge()."&amp;commentId=").$commList[$i]->getId();
			
			$list[] = $listItem;
		}
		return $list;
	}
        private static function convertSolutionComments($commList)
	{
		$list = array();
		$userRep = new UserRepository();
		
		for($i = 0; $i < count($commList); $i++)
		{
			$user = $userRep->getById($commList[$i]->getIdAuthor());
			$userName = $user->getUserName();
			$listItem = array();
			$listItem['userId'] = $commList[$i]->getIdAuthor();
			$listItem['name'] = $userName;
			$listItem['image'] = $user->getImage();
			$listItem['datetime'] = $commList[$i]->getDate();
			$listItem['message'] = "$userName has commented one of your solutions.";
			$listItem['url'] = Utils::getUrl("solution/view.php?id=".$commList[$i]->getIdSolution()."&amp;commentId=").$commList[$i]->getId();
			
			$list[] = $listItem;
		}
		return $list;
	}
	private static function convertSolutions($solList)
	{
		$list = array();
		$userRep = new UserRepository();
		
		for($i = 0; $i < count($solList); $i++)
		{
			$user = $userRep->getById($solList[$i]->getIdAuthor());
			$userName = $user->getUserName();
			$listItem = array();
			$listItem['userId'] = $solList[$i]->getIdAuthor();
			$listItem['name'] = $userName;
			$listItem['image'] = $user->getImage();
			$listItem['datetime'] = $solList[$i]->getDate();
			$listItem['message'] = "$userName has solved one of your challenges.";
			$listItem['url'] = Utils::getUrl("solution/view.php?id=").$solList[$i]->getId();
			
			$list[] = $listItem;
		}
		return $list;
	}
	private static function convertMessages($msgList)
	{
		$list = array();
		$userRep = new UserRepository();
		
		for($i = 0; $i < count($msgList); $i++)
		{
			$user = $userRep->getById($msgList[$i]->getIdSender());
			$userName = $user->getUserName();
			$listItem = array();
			$listItem['userId'] = $msgList[$i]->getIdSender();
			$listItem['name'] = $userName;
			$listItem['image'] = $user->getImage();
			$listItem['datetime'] = $msgList[$i]->getDate();
			$listItem['message'] = "$userName has left a message.";
			$listItem['url'] = Utils::getUrl("message/view.php?id=").$msgList[$i]->getId();
			
			$list[] = $listItem;
		}
		return $list;
	}
}
?>